﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using System.Reflection;
using System.Data;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;
using T.FAS.Runtime.Base.Common;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.Base.Dao
{
    public class QueryPlanDefaultDao : DaoService<QueryPlanDefault>
    {
        public override string TableName => "FAS_QueryPlanDefault";
        public override string IDColumnName => "ID";
        public override List<string> ExceptColumnName => new List<string> { };

        public QueryPlanDefaultDao() : base()
        {
            DBName = "TFAS";
        }
        public override QueryPlanDefault ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            myCol.Remove("IsDefault");
            var data = base.ConvertToObject(myCol, row);
            if (row["IsDefault"] != DBNull.Value)
                data.IsDefault = Convert.ToBoolean(row["IsDefault"]);
            return data;
        }
        /// <summary>
        /// 获取所有默认方案信息
        /// </summary>
        public List<QueryPlanDefault> GetQueryPlanDefaultList(string userID)
        {
            var result = new List<QueryPlanDefault>();
            List<object> objects = base.GetObjectsByFilter($"UserID='{userID}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as QueryPlanDefault));
            }
            return result;
        }
        /// <summary>
        /// 删除用户方案信息
        /// </summary>
        public void DeletePlan(string planId)
        {
            List<IDbDataParameter> paramList = new List<IDbDataParameter>();
            paramList.Add(DB.MakeInParam("@planId", TDbDataType.VarChar, planId));
            var sql = $"delete from {TableName} Where PlanID=@planId;";
            DB.ExecSqlStatement(sql, paramList.ToArray());
        }
        /// <summary>
        /// 获取是否默认
        /// </summary>
        public bool GetQueryPlanDefault(string planId)
        {
            List<IDbDataParameter> paramList = new List<IDbDataParameter>();
            paramList.Add(DB.MakeInParam("@planId", TDbDataType.VarChar, planId));
            var sql = string.Format($@"select 1 from {TableName} where planId=@planId");
            var ds = DB.ExecuteDataSet(sql, paramList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 设置默认
        /// </summary>
        public void DefaultQueryPlan(QueryPlan plan, bool isDefault)
        {
            var userId = UserUtil.GetCurrentUserID();
            List<IDbDataParameter> paramList = new List<IDbDataParameter>
            {
                DB.MakeInParam("@dqID", TDbDataType.VarChar, plan.DQID),
                DB.MakeInParam("@pageID", TDbDataType.VarChar, plan.PageID),
                DB.MakeInParam("@businessTag", TDbDataType.VarChar, plan.BusinessTag)
            };
            string sql = $@"delete from 
FAS_QueryPlanDefault 
where pageid=@pageID 
{(string.IsNullOrEmpty(plan.DQID) ? "AND (DQID = '' or DQID is null)" : "and DQID=@dqID ")}
{(string.IsNullOrEmpty(plan.BusinessTag) ? "AND (BusinessTag = '' or BusinessTag is null)" : "and BusinessTag=@businessTag ")}
and UserID='{userId}'";
            base.DB.ExecSqlStatement(sql, paramList.ToArray());
            if (isDefault)
            {
                var planDefault = new QueryPlanDefault();
                planDefault.ID = Guid.NewGuid().ToString();
                planDefault.PlanID = plan.ID;
                planDefault.UserID = userId;
                planDefault.PageID = plan.PageID;
                planDefault.DQID = plan.DQID;
                planDefault.IsDefault = true;
                planDefault.BusinessTag = plan.BusinessTag;
                base.Add(new List<object> { planDefault });
            }
        }
    }
}
